/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import Controller.UCMenuOption;
import Controller.UCNum_HorzLanes;
import Controller.UCNum_VertLanes;
import Controller.UCProb_HorzCars;
import Controller.UCProb_VertCars;
import Controller.UCRun_Stop;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * This is the main menu on the text based screen.
 * @author Luke
 */
public class Menu {

    private ArrayList<UCMenuOption> menuOptions = new ArrayList();
    Scanner menuChoice = new Scanner(System.in); //make a Scanne robject for user input
    ArrayList streetValues = new ArrayList();

    /**
     *Constructor for the menu, sets up values
     */
    public Menu() {
        makeMenu();
        streetValues.add(2);
        streetValues.add(2);
        streetValues.add(0.5);
        streetValues.add(0.5);
        System.out.println("----------------Welcome to the Traffic Sim V2---------------- \n");

    }

    /**
     * The make menu method, makes all the menu options and sets their text values
     */
    public void makeMenu() {

        String[] theOptions = {
            "1. Set Number of horizontal lanes (H-street) [min 1, default=2, max 3]",
            "2. Set Number of vertical lanes (V-street) [min 1, default=2,max 4]",
            "3. Set Probability of a car entering H-street [min 0, default=0.5, max 1]",
            "4. Set Probability of a car entering V-street [min 0, default=0.5, max 1]",
            "5. Run/Stop simulation."}; //Array fo the text for each menu option

        menuOptions.add(new UCNum_HorzLanes()); //set menuoptions
        menuOptions.add(new UCNum_VertLanes());
        menuOptions.add(new UCProb_HorzCars());
        menuOptions.add(new UCProb_VertCars());
        menuOptions.add(new UCRun_Stop());

        int counter = 0;
        for (UCMenuOption option : menuOptions) { //Goes through each menu options and prints it
            option.set_MenuOption(theOptions[counter]);
            counter++;
        }

    }

    /**
     * Method for printing just the menu, called in recursive menu structure to re display uses choices
     */
    public void printMenu() {
        System.out.println("---MENU---");
        for (UCMenuOption option : menuOptions) { //Goes through each menu options and prints it
            System.out.println(option.get_MenuOption());
        }
        System.out.println("Enter your choice>");
    }

    /**
     * The input is given then this method is called the analyse the input and call the
     * appropriate menu options with the users choice passed in.
     * @param usersChoice Is given by the user and passed straight into the menu options
     */
    public void analyseChoice(int usersChoice) {
        switch (usersChoice) {
            case 1:
                streetValues.set(0, menuOptions.get(usersChoice - 1).run());
                break;
            case 2:
                streetValues.set(1, menuOptions.get(usersChoice - 1).run());
                break;
            case 3:
                streetValues.set(2, menuOptions.get(usersChoice - 1).run());
                break;
            case 4:
                streetValues.set(3, menuOptions.get(usersChoice - 1).run());
                break;
            case 5:
                System.out.println(menuOptions.get(usersChoice - 1).run());;
                break;
            default:
                System.out.println("The selection made was not between 1 and 5. Try Again");
                runMenu();
                break;
        }
    }

    /**
     *Controls the user input and passes it accordingly, whilst also returning the intersection
     * specific values used by the coordinator object.
     * @return
     */
    public ArrayList runMenu() {
        printMenu(); //print the menu
        String userInput = menuChoice.nextLine(); //the users choice
        boolean acceptable = true;
        int int_userInput = 0;

        try {
            int_userInput = Integer.parseInt(userInput); //make the input into a int
            acceptable = true;
        } catch (Exception e) {
            acceptable = false;
            System.out.println("The input provided was not an integer");

        }
        if (acceptable && int_userInput != 5) {
            analyseChoice(int_userInput); //analyse the input
            runMenu();
        } else if (acceptable && int_userInput == 5){
            analyseChoice(int_userInput); //for the time being when 5 is called do nothing
        }

        else {
            runMenu(); //invalid input so run the menu again
        } return streetValues;
    }
}
